home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / getline.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  2KB  |  87 lines

  1. /* getline.c version 1.0 PA0GRI */
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include "global.h"
  5.  
  6. /*
  7.  *    getline() 
  8.  *    to read a line from a domain file and return a usefull line
  9.  *    completely assembled (if multy line)
  10.  *    It skips any connent lines. It follows rfc 1133 , 1134 , 1135 , 1136.
  11.  */
  12. char *
  13. #ifdef PROTOTYPES
  14. getline(FILE *fp)
  15. #else
  16. getline(fp)
  17. FILE *fp;
  18. #endif
  19. {
  20.     char *line;
  21.     char *contline;
  22.     char *chrptr1;
  23.     char *chrptr2;
  24.     int s1,s2;
  25.     int loop = 1;
  26.  
  27.     if(fp == NULLFILE)
  28.         return NULLCHAR;    /* just in case */
  29.     line = mallocw(256);        /* get buffer space */
  30. #ifdef __GNUC__
  31.     s1 = 0;            /* this one may be for real... */
  32. #endif
  33.     while(loop){
  34.         if(fgets(line,256,fp) == NULL){
  35.             free(line);
  36.             return NULLCHAR;
  37.         }
  38.         if(line[0] == '#' || line[0] == ';')
  39.             continue;    /* skip comment lines */
  40.         if((s1 = strlen(line)) < 2)
  41.             continue;    /* empty line */
  42.         loop = 0;        /* none of above */
  43.     }
  44.     line[s1-1] = '\0';        /* kill nl */
  45.     if((chrptr1 = strchr(line,';')) != NULLCHAR){
  46.         *chrptr1 = '\0';    /* eliminate comment */
  47.         s1 = strlen(line);    /* recompute line size */
  48.     }
  49.     if((chrptr1 = strchr(line,'(')) != NULLCHAR){ /* continuation */
  50.         *chrptr1 = ' ';            /* replace with space */
  51.         if((chrptr2 = strchr(line,')')) != NULLCHAR){ /* continuation */
  52.             *chrptr2 = ' ';        /* replace with space */
  53.             return line;        /* complete line */
  54.         }
  55.         loop = 1;
  56.     }
  57.     contline = mallocw(256);    /* get buffer space */
  58.     while(loop){
  59.         if(fgets(contline,256,fp) == NULL){
  60.             free(line);
  61.             free(contline);
  62.             return NULLCHAR;
  63.         }
  64.         s2 = strlen(contline);
  65.         contline[s2-1] = '\0';    /* kill nl */
  66.         if(contline[0] == '#' || contline[0] == ';')
  67.             continue;    /* skip comment lines */
  68.         if((s2 = strlen(contline)) < 2)
  69.             continue;    /* empty line */
  70.         if((chrptr1 = strchr(contline,';')) != NULLCHAR){
  71.             *chrptr1 = '\0';    /* eliminate comment */
  72.             s2 = strlen(contline);    /* recompute line size */
  73.         }
  74.         chrptr1 = mallocw((unsigned)s1+s2+2);
  75.         sprintf(chrptr1,"%s %s",line,contline);
  76.         free(line);
  77.         line = chrptr1;
  78.         if((chrptr2 = strchr(line,')')) != NULLCHAR){ /* continuation */
  79.             *chrptr2 = ' ';        /* replace with space */
  80.             loop = 0;
  81.         }
  82.  
  83.     }
  84.     free(contline);
  85.     return line;
  86. }
  87.